package com.maila.xiaoaobusiness.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.StreamCorruptedException;

/**
 * Created by mark on 2017/9/23.
 */
public class PrefUtil {


    private static SharedPreferences mPref;
    private static Editor mEditor;

    public static final String APPLICATION_PREFERENCES = "maila_prefs";

    private static void initPref(Context context) {

        if (null == mPref) {
            mPref = context.getSharedPreferences(APPLICATION_PREFERENCES,
                    Context.MODE_PRIVATE);
        }
    }

    public static void removePref(Context context, String prefKey) {

        initPref(context);
        mEditor = mPref.edit();
        mEditor.remove(prefKey);
        mEditor.commit();
    }

    /**
     * 保存字符串Pref
     *
     * @param key
     * @param value
     */
    public static void savePref(Context context, String key, String value) {

        initPref(context);
        mEditor = mPref.edit();
        mEditor.putString(key, value);
        mEditor.commit();
    }

    /**
     * 保存布尔值Pref
     *
     * @param key
     * @param value
     */
    public static void savePref(Context context, String key, boolean value) {

        initPref(context);
        mEditor = mPref.edit();
        mEditor.putBoolean(key, value);
        mEditor.commit();
    }

    /**
     * 保存整形Pref
     *
     * @param key
     * @param value
     */
    public static void savePref(Context context, String key, int value) {
        initPref(context);
        mEditor = mPref.edit();
        mEditor.putInt(key, value);
        mEditor.commit();
    }

    /**
     * 保存整形Pref
     *
     * @param key
     * @param value
     */
    public static void savePref(Context context, String key, long value) {
        initPref(context);
        mEditor = mPref.edit();
        mEditor.putLong(key, value);
        mEditor.commit();
    }

    /**
     * 获取pref中key对应的字符串值
     *
     * @param key
     * @return
     */
    public static String getStringPref(Context context, String key, String defaultValue) {

        initPref(context);
        return mPref.getString(key, defaultValue);
    }

    /**
     * 获取pref中key对应的布尔值
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static boolean getBooleanPref(Context context, String key, boolean defaultValue) {

        initPref(context);
        return mPref.getBoolean(key, defaultValue);
    }

    /**
     * 获取pref中key对应的整形
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getIntPref(Context context, String key, int defaultValue) {

        initPref(context);
        return mPref.getInt(key, defaultValue);
    }

    /**
     * 获取pref中key对应的整形
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static long getLongPref(Context context, String key, long defaultValue) {

        initPref(context);
        return mPref.getLong(key, defaultValue);
    }

    /**
     * 获取pref中key对应的整形
     *
     * @param key
     * @return
     */
    public static Object getObjectPref(Context context, String key) {

        Object object = null;
        String objectBase64 = PrefUtil.getStringPref(context, key, "");
        if ("".equals(objectBase64)) {
            return null;
        }
        //读取字节
        byte[] csBase64 = Base64.decode(objectBase64.getBytes(), Base64.DEFAULT);
        //封装到字节流
        ByteArrayInputStream bais = new ByteArrayInputStream(csBase64);
        try {
            //再次封装
            ObjectInputStream bis = new ObjectInputStream(bais);
            try {
                //读取对象
                object = bis.readObject();
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return object;
    }

}
